package com.dinzeer.slashblade_useful_addon.events;

import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.event.SlashBladeEvent;
import mods.flammpfeil.slashblade.registry.SpecialEffectsRegistry;
import mods.flammpfeil.slashblade.registry.specialeffects.SpecialEffect;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class WitherEdgeRiseUp {

    public static boolean hasSpecialEffect(ItemStack stack, String effect,String Modid) {
        CompoundTag tag = stack.getOrCreateTag(); // 获取或创建NBT标签
        effect= Modid+":"+effect;
        if (tag.contains("bladeState")) { // 检查是否存在ForgeCaps标签
            CompoundTag forgeCaps = tag.getCompound("bladeState");

            if (forgeCaps.contains("SpecialEffects")) { // 检查SpecialEffects标签
                ListTag specialEffects = forgeCaps.getList("SpecialEffects", 8); // 8表示String类型
                for (int i = 0; i < specialEffects.size(); i++) {
                    String currentEffect = specialEffects.getString(i);
                    if (effect.equals(currentEffect)) {
                        return true; // 找到了指定的特殊效果
                    }
                }

            }

        }
        return false; // 没有找到指定的特殊效果
    }


    @SubscribeEvent
    public static void riseUp(LivingHurtEvent event) {

        if (!(event.getSource().getEntity() instanceof Player player)){
            return;
        }
        if (!(hasSpecialEffect(player.getMainHandItem(),"wither_edge",SlashBlade.MODID))){
            return;
        }
        int level=player.experienceLevel;

        if(SpecialEffect.isEffective(SpecialEffectsRegistry.WITHER_EDGE.get(),level)){
            event.getEntity().addEffect(new MobEffectInstance(MobEffects.WITHER, 100, 1));
            System.out.println("附着成功");
        }


    }





}
